package com.luych.oauth2Server.service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.luych.oauth2Server.entity.UserInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.List;

@Service
public class UserService {

    private final RestTemplate restTemplate;
    private final ObjectMapper objectMapper;

    @Autowired
    public UserService(RestTemplate restTemplate, ObjectMapper objectMapper) {
        this.restTemplate = restTemplate;
        this.objectMapper = objectMapper;
    }

    public UserInfo getUserByLoginName(String loginName) throws IOException {
        if (StringUtils.isEmpty(loginName)) return null;
        String response =  restTemplate.getForObject("http://USERCENTER/user/by/loginName/{1}", String.class, loginName);
        return  objectMapper.readValue(response, UserInfo.class);
    }


}
